package cn.life.business.project.dao.impl;

import cn.life.business.project.dao.ProjectDao;
import cn.life.business.project.entity.po.ProjectPo;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description:    项目Dao实现层
 *
 * @Author 李佳明
 * @Date 2018-07-24 15:57
 */
@Repository
public class ProjectDaoImpl extends AbstractSupportDao implements ProjectDao {

    @Override
    public Long addProject(ProjectPo projectPo) {
        Long id = queryTableNextId("project_info","project_id");
        String sql = "insert into project_info(project_id,project_name,project_addr,project_desc,factory_id,opr_user_id,opr_user_name,opr_date,start_date,end_date,person_liable_name,project_money,factory_name,project_no) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql,id,projectPo.getProjectName(),projectPo.getProjectAddr(),projectPo.getProjectDesc(),projectPo.getFactoryId(),projectPo.getOprUserId(),projectPo.getOprUserName(),getNowDate(),projectPo.getStartDate(),projectPo.getEndDate(),projectPo.getPersonLiableName(),projectPo.getProjectMoney(),projectPo.getFactoryName(),getProjectNo(projectPo.getFactoryId(),id)) == 1 ? id : null;
    }

    @Override
    public int updateProject(ProjectPo projectPo) {
        String sql = "update project_info set project_name = ?,project_addr = ?,project_desc = ?,project_money = ?,person_liable_name = ?,start_date = ?,end_date = ? where project_id = ?";
        return update(sql,projectPo.getProjectName(),projectPo.getProjectAddr(),projectPo.getProjectDesc(),projectPo.getProjectMoney(),projectPo.getPersonLiableName(),projectPo.getStartDate(),projectPo.getEndDate(),projectPo.getProjectId());
    }

    @Override
    public int deleteProject(ProjectPo projectPo) {
        String sql = "delete from project_info where project_id = ?";
        return update(sql,projectPo.getProjectId());
    }

    @Override
    public Pagination<ProjectPo> queryProjectList(ProjectPo projectPo) {
        String sql = "select * from project_info where factory_id = ?";
        if(projectPo.getProjectId() != null && !projectPo.getProjectId().equals("")){
            sql = sql + " and project_id = " + projectPo.getProjectId() + " ";
        }
        if(projectPo.getProjectName() != null && !projectPo.getProjectName().equals("")){
            sql = sql + " and project_name like '%" + projectPo.getProjectName() + "%'";
        }
        sql=sql+" ORDER BY opr_date DESC ";
        return queryForListByPageEx(sql,ProjectPo.class,projectPo.getPageIndex(),projectPo.getPageSize(),projectPo.getFactoryId());
    }

    @Override
    public ProjectPo queryProjectDetail(ProjectPo projectPo) {
        String sql = "select * from project_info where project_id = ?";
        return queryForObject(sql,ProjectPo.class,projectPo.getProjectId());
    }

    @Override
    public List<UserInfo> getProjectUserList(ProjectPo projectPo) {
        String sql = "select * from user_role where relation_state = 1 and project_id = ?";
        return queryForList(sql,UserInfo.class,projectPo.getProjectId());
    }
}
